package com.googlecode.openauth.web.interceptor;

import java.util.Map;

import org.apache.log4j.Logger;

import com.googlecode.openauth.web.entity.UserInfo;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginedInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4173278951973411096L;

	private static Logger logger = Logger.getLogger(LoginedInterceptor.class);

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// 获取session中的username属性

		Map<String, Object> session = invocation.getInvocationContext()
				.getSession();

		Object object = session.get("user");

		if (object == null) {
			logger.info(" user is not login ! object is null ");
			return Action.LOGIN;
		}

		if (!(object instanceof UserInfo)) {
			logger.info(" user is not login ! not instance ");
			return Action.LOGIN;
		}

		UserInfo userInfo = (UserInfo) object;
		long userId = userInfo.getUserId();
		if (userId == 0) {
			logger.info(" user is not login ! not instance ");
			return Action.LOGIN;
		}
		return invocation.invoke();
	}

}
